package org.fuys.etp.core.pool.algorithm;

/**
 * @projectName: fuys-etp-final
 * @package: org.fuys.etp.core.pool.algorithm
 * @className: PIDController
 * @author: WangJing
 * @description: PID控制器
 * @date: 2025/2/10 21:19
 * @version: 1.0
 */
public class PIDController {
    private final double target;
    private double lastError = 0.0;
    private double integral = 0.0;

    private final double Kp;
    private final double Ki;
    private final double Kd;

    public PIDController(double target, double Kp, double Ki, double Kd) {
        this.target = target;
        this.Kp = Kp;
        this.Ki = Ki;
        this.Kd = Kd;
    }

    public double calculate(double currentUsage) {
        double error = target - currentUsage;
        integral+=error;
        double derivative = error - lastError;
        lastError = error;

        //输出正数则需要扩容 负数为缩容
        return Kp*error+Ki*integral+Kd*derivative;
    }
}
